// pages/my/mybooking/mybooking.js

Page({


  /**
   * 页面的初始数据
   */
  data: {
    tabPanelstyle: 'display:flex;justify-content:center;align-items:center;',
    content: '取消预约将扣除50%的费用，确定取消吗？',
    // 页面信息
    pageList: [],
    surplusTime: [],
    reservationStatus: 1,
  },
  // 查询私课请求--根据用户id查询全部
  getList() {
    wx.request({
      url: 'http://localhost:8080/keep/reservation/selectTeamReservationListByUserId',
      method: 'GET',
      data: {
        reservationStatus: this.data.reservationStatus,
        userId: 10001
      },
      header: {
        'Authorization': getApp().globalData.authorization
      },
      success: resp => {
        this.setData({
          pageList: resp.data.rows
        })
        console.log('私课请求--根据用户id查询全部:', this.data.pageList);
        if (this.data.pageList == null) {
          return;
        }
        this.data.pageList.forEach(item => {
          var reservationTime = new Date(item.reservationTime);
          var now = new Date();
          if (now > reservationTime) {
            // 如果当前时间已经超过指定时间
            this.setData({
              surplusTime: this.data.surplusTime.push('')
            })
            return;
          }
          const hours = Math.floor((reservationTime - now) / (1000 * 60 * 60));
          const surplusTime = this.data.surplusTime;
          surplusTime.push(hours + 11);
          this.setData({
            surplusTime: surplusTime
          })
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
  },
  // 切换订单状态
  onTabsChange(event) {
    this.setData({
      reservationStatus: event.detail.value
    })
    this.getList()
  },
  onTabsClick(event) {},

  gocancle(e) {
    console.log(e.currentTarget.dataset.index);
    wx.showModal({
      title: '提示',
      content: this.data.content,
      success: resp => {
        if (resp.confirm) {
          wx.request({
            url: 'http://localhost:8080/keep/reservation/updateReservationStatus',
            method: 'PUT',
            data: e.currentTarget.dataset.index,
            header: {
              'Authorization': getApp().globalData.authorization
            },
            success: resp => {
              if (resp.data.code == 200) {
                wx.showToast({
                  title: '取消预约成功，待退款中...',
                  icon: 'success',
                  duration: 2000
                })
                this.getList()
              } else {
                wx.showToast({
                  title: '申请退款失败',
                  icon: 'loading',
                  duration: 2000
                })
              }
            }
          })
        } else if (resp.cancel) {
          console.log('用户点击了取消');
        }
        this.getList();
      }
    });
  },
  // 签到
  goSign(e) {
    var reservationTime = new Date(e.currentTarget.dataset.index.reservationTime); 
    reservationTime.setHours(19, 0, 0, 0);
    if (new Date() > reservationTime) {
      wx.showModal({
        title: '提示',
        content: '是否签到该课程呢?',
        success: resp=> {
          if (resp.confirm) {
            wx.request({
              url: 'http://localhost:8080/keep/reservation/updateReservationStatusSign',
              method: 'PUT',
              data: e.currentTarget.dataset.index,
              header: {
                'Authorization': getApp().globalData.authorization
              },
              success: resp => {
                if (resp.data.code == 200) {
                  wx.showToast({
                    title: '签到成功',
                    icon: 'success',
                    duration: 2000
                  })
                  this.getList()
                } else {
                  wx.showToast({
                    title: '签到失败',
                    icon: 'loading',
                    duration: 2000
                  })
                }
              }
            })
          } else if (resp.cancel) {
            console.log('用户点击了取消');
          }
          this.getList();
        }
      });
      
    } else {
      wx.showModal({
        title: '提示',
        content: '课程时间还未到哦！'
      })
    }
    

  },
  // 返回上一页
  onNavigate() {
    wx.navigateBack({
      delta: 1
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})